<template>
	<view class="">
		<scroll-view scroll-y @scrolltolower="lower" style="height:calc(100vh - 100px);" @touchstart="touchStart"
			@touchend="touchEnd" @touchmove="touchMove">

			<view v-if="showRefresh" style='width:100%;position:relative;padding:60rpx 0;'>
				<view class="text-gray" style='position: absolute;left: 50%;top: 50%;transform: translate(-50%, -50%);'>
					<view v-if="freshStatus == 'fresh'" class="flex">
						<view class="lzy-loading"></view>
						<view>刷新中...</view>
					</view>
					<view class="text" v-else-if="freshStatus == 'more'">
						<text class="cuIcon-refresharrow"></text> 继续下拉刷新
					</view>
					<view class="text" v-else>
						释放刷新
					</view>
				</view>
			</view>

			<view v-for="(item, index) in list.rows" :key="item.ancId" @tap="detail(item.ancId)">
				<view class="margin-sm bg-white padding radius">
					<view class="flex">
						<view class="flex-sub text-blue">
							<text v-if="'1'==item.recruitType">招聘信息</text>
							<text v-else>求职信息</text>
							<!--
					<text @tap.stop="push(item.ancId)">我要置顶推送</text> 
					-->
						</view>
						<view class="flex-twice text-right text-red">
							
							<view class="flex-sub text-lg ">
								<template v-if="'1'==item.isUrgent">
									<text class="margin-right-sm">
										<template v-if="item.recruitType==1">{{'急聘'}}</template>
										<template v-else="item.recruitType==0">{{'加急'}}</template>
									</text>
								</template>
								 <template v-if="'0'==item.isUrgent&&'1'==item.isTop">
									<text class="margin-right-sm text-blue">置顶</text>
								</template> 
								<template v-if="0==item.state">
									<text class="text-orange">审核中</text>
								</template>
								<template v-else-if="1==item.state">
									<text class="text-blue">展示中</text>
								</template>
								<template v-else-if="3==item.state">
									<text class="text-gray">已关闭</text>
								</template>
								<template v-else-if="4==item.state">
									<text class="text-gray">审核拒绝</text>
								</template>
							</view>
							<!--
					<text @tap.stop="refresh(item)" class="cuIcon-refresh margin-right">刷新</text>
					<text @tap.stop="edit(item)" class="cuIcon-edit margin-left margin-right"></text>
					<text @tap.stop="del(item, index)" class="cuIcon-delete margin-left text-red"></text>
					-->
						</view>
					</view>
						<view class="flex-sub text-bold padding-top-xs  multi-line">
						{{item.title}}
						</view>
					
					
					<view class="flex padding-top-sm">
						
						<view class="flex-sub">
							<text class="text-red">{{item.salary?item.salary:'面谈'}}</text> /月
						</view>
						<view class="flex-sub">
							<view class="cu-tag radius line-blue">{{item.dtypeName}}</view>
							<view v-if="item.dlTypeName!='不限'" class="cu-tag radius line-blue">{{item.dlTypeName}} 驾照
							</view>
						</view>
					</view>
					<!--<view class="flex padding-top-sm text-sm">
						 <view style="line-height: 15px;" class="flex-sub text-gray padding-top-xs">
							{{ subTm(item.creTm) }}
						</view>
						<view style="line-height: 15px;" class="flex-sub text-gray padding-top-xs">
							{{ item.cityName }}
						</view> 
					</view>-->
					<view v-if="'1'==item.state" style="border-top: 1px solid #eee;" class="padding-top-sm">
						<view class="flex">
							<view class="flex-sub">
								<text class="text-gray">{{ subTm(item.creTm) }}</text>
								<text class="text-gray margin-left-xs">{{ item.cityName }}</text>
							</view>
							<view class="flex-sub">
								<text @tap.stop="refresh(item)" style="border: 1px solid #eee;border-radius: 5px;padding:0 20px;" class="cu-btn sm text-sm bg-white margin-right">
									刷新
								</text>
								<text @tap.stop="push(item.ancId)" style="border: 1px solid #eee;border-radius: 5px;padding:0 20px;" class="cu-btn sm text-sm bg-red margin-right">
									加急
								</text>
							</view>
							
						</view>
						
					</view>
				</view>
			</view>

			<template v-if="!showRefresh">
				<view v-if="isLoad" class="cu-load text-gray loading"></view>
				<view v-else-if="list.rows.length==0" class="pos-center text-center text-gray">
					<text class="cuIcon-activityfill" style="font-size: 30px;"></text>
					<view class="">
						<template v-if="state">
							暂无数据
						</template>
						<template v-else>
							你还没有发布过信息哦，快去发布一条吧。
						</template>
					</view>
				</view>
				<view v-else class="cu-load text-gray over"></view>
			</template>
		</scroll-view>
		<member :id="ancId" :showModal="showModal" @close="showModal = false" />
	</view>
</template>

<script>
	import {
		displayTime
	} from '../utils/util.js';
	import member from './member.vue'
	export default {
		name: "tasklist",
		props: {
			state: {
				type: String, // 信息类型
				value: 0
			},
			timastap: {
				type: String, // 
				value: ''
			},
		},
		data() {
			return {
				ancId: null,
				showModal: false,
				queryData: {
					startDt: '',
					endDt: '',
					currPage: 1,
					creID: '',
					state: ''
				},
				list: {
					total: 0,
					rows: []
				},
				isLoad: false,
				freshStatus: 'more',
				showRefresh: false,
			}
		},
		// 组件生命周期
		created: function(e) {
			// 0未审核  1 展示中  3已关闭  4 审核拒绝
			switch (this.state) {
				case '0':
					this.queryData.state = '';
					break;
				case '1':
					this.queryData.state = '1';
					break;
				case '2':
					this.queryData.state = '0';
					break;
				case '3':
					this.queryData.state = '3';
					break;
				case '4':
					this.queryData.state = '4';
					break;
			}
			let u = uni.getStorageSync('ONLINE_USER');
			if (u) {
				this.queryData.creID = u.userID;
				this.getList();
			}
		},
		components: {
			member
		},
		watch: {
			timastap(val, oldVal){
				this.getList()
			}
		},
		methods: {
			subTm(str) {
				return str.substr(0, 10)
			},
			push(ancId) {
				this.ancId = ancId
				this.showModal = true
			},
			// 编辑
			edit(item) {
				uni.setStorageSync('ancInfo', item);
				uni.navigateTo({
					url: '/pages/user/pubedit'
				})
			},
			// 刷新
			refresh(item) {
				uni.showLoading({
					mask: true,
					title: '请稍候...'
				})
				this.baseRequest('api/driver/info/refresh', 'PUT', {
					ancId: item.ancId
				}, (res) => {
					uni.hideLoading();
					setTimeout(() => {
						uni.showToast({
							title: '刷新成功'
						});
						setTimeout(() => {
							item.creTm = res;
						}, 1000)
					}, 0);
				});
			},
			lower() {
				if (this.list.rows.length < this.list.total) {
					this.queryData.currPage += 1;
					this.getList();
				}
			},
			del(item, index) {
				let _this = this;
				uni.showModal({
					title: '提示',
					content: '确认删除该信息？',
					success: function(res) {
						if (res.confirm) {
							uni.showLoading({
								mask: true,
								title: '请稍候...'
							})
							_this.baseRequest('api/driver/info', 'DELETE', {
								ancId: item.ancId
							}, function(res, info) {
								uni.hideLoading();
								setTimeout(() => {
									uni.showToast({
										title: '删除成功'
									});
									setTimeout(() => {
										uni.hideToast();
										_this.list.rows.splice(index, 1);
									}, 1000)
								}, 0);
							});
						}
					},
				});
			},
			// 取消
			cancel(orderId, text) {
				let _this = this;
				let content = '确认' + text + '订单？';
				uni.showModal({
					title: '提示',
					content: content,
					success: function(res) {
						if (res.confirm) {
							_this.baseRequest('app/bus/custorder/delete/' + orderId, 'DELETE', {}, function(
								res, info) {
								uni.hideLoading();
								setTimeout(() => {
									uni.showToast({
										title: text + '成功'
									});
									setTimeout(() => {
										uni.hideToast();
										_this.getList();
									}, 1000)
								}, 0);
							});
						}
					},
				});
			},
			//
			// 请求列表
			getList: function(obj) {
				this.isLoad = true;
				let _this = this;
				setTimeout(() => {
					let _url = 'api/driver/info/mypub';
					this.baseRequest(_url, 'GET', this.queryData, (res) => {
						if (res) {
							if (_this.queryData.currPage != 1) {
								_this.list.rows = _this.list.rows.concat(res.rows);
								_this.list.total = res.total;
							} else {
								_this.list = res;
							}
						} else {
							_this.list = {
								total: 0,
								rows: []
							};
							_this.$emit('nodata', {})
						}
						_this.isLoad = false;
						_this.showRefresh = false;
					});
				}, 500);
			},
			//
			detail(ancId) {
				uni.navigateTo({
					url: '/pages/user/detail?id=' + ancId
				})
			},
			// 触摸开始位置
			touchStart(e) {
				this.startY = e.changedTouches[0].pageY;
				this.freshStatus = 'more';
			},
			// 
			touchMove(e) {
				if (this.pullStatus == 'loading') {
					return;
				}
				let endY = e.changedTouches[0].pageY;
				let startY = this.startY;
				let dis = endY - startY;
				// 判断是否下拉
				if (dis <= 0) {
					return;
				}
				let offsetTop = e.currentTarget.offsetTop;
				if (dis > 20) {
					this.showRefresh = true;
					if (dis > 50) {
						this.freshStatus = 'end';
					} else {
						this.freshStatus = 'more';
					}
				} else {
					this.showRefresh = false;
				}
			},
			// 触摸结束位置
			touchEnd(e) {
				if (this.freshStatus == 'end') {
					this.freshStatus = 'fresh';
					this.queryData.currPage = 1
					setTimeout(() => {
						this.getList();
					}, 500);
				} else {
					this.showRefresh = false
				}
			}
		}
	}
</script>
